package com.design.movieticket.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.design.movieticket.entity.*;
import com.design.movieticket.mapper.*;
import com.design.movieticket.service.RoleService;
import com.design.movieticket.service.SeatService;
import org.springframework.data.convert.EntityWriter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SeatServiceImpl extends ServiceImpl<SeatMapper, Seat> implements SeatService {
    @Resource
    private SeatMapper seatMapper;

    @Resource
    private MovieHallMapper movieHallMapper;

    @Resource
    private MovieArrangeMapper movieArrangeMapper;

    @Resource
    private OrdersMapper ordersMapper;

    /** 获取座位数组 */
    @Override
    public Seat[][] getSeatList(Long movieHallId) {
        MovieHall movieHall = movieHallMapper.selectById(movieHallId);
        int rowsSeatCount = movieHall.getRowsSeatCount();
        int colsSeatCount = movieHall.getColsSeatCount();
        Seat[][] seatList = new Seat[rowsSeatCount][colsSeatCount];
        List<Seat> seat = seatMapper.getSeatList(movieHallId);
        int z = 0;
        for(int i=0 ; i<rowsSeatCount ; i++){
            for (int j=0 ; j<colsSeatCount ; j++){
                seatList[i][j] = seat.get(z);
                z++;
            }
        }
        return seatList;
    }

    /** 新增或保存座位 */
    @Override
    public void addOrUpdateSeatList(List<Seat> seat) {
        if (seat.size()==0){
            return;
        }
        long movieHallId = seat.get(0).getMovieHallId();
        List<Seat> oldSeat = seatMapper.getSeatList(movieHallId);
        List<Seat> oldRemoveSeat = seatMapper.getSeatList(movieHallId);
        int maxRowsSeatCount = 0;
        int maxColsSeatCount = 0;

        for (int i = 0; i<seat.size(); i++){
            if (seat.get(i).getSeatX()>maxRowsSeatCount){
                maxRowsSeatCount = seat.get(i).getSeatX();
            }
            if (seat.get(i).getSeatY()>maxColsSeatCount){
                maxColsSeatCount = seat.get(i).getSeatY();
            }
        }

        //新增or更新
        for (int i = 0; i<seat.size(); i++){
            if (seat.get(i).getId() == null){
                super.saveOrUpdate(seat.get(i));
                oldRemoveSeat.remove(seat.get(i));
            }
            for (int j = 0; j<oldSeat.size(); j++){
                if (seat.get(i).getId().equals(oldSeat.get(j).getId())){
                    super.saveOrUpdate(seat.get(i));
                    oldRemoveSeat.remove(seat.get(i));
                    continue;
                }
            }
        }

        //删除
        for (int i = 0; i<seat.size(); i++){
            for (int j = 0; j<oldRemoveSeat.size(); j++){
                if (seat.get(i).getId().equals(oldRemoveSeat.get(j).getId())){
                    oldRemoveSeat.remove(oldRemoveSeat.get(j));
                    continue;
                }
            }
        }
        for (int i = 0; i<oldRemoveSeat.size(); i++){
            super.removeById(oldRemoveSeat.get(i).getId());
        }

        MovieHall movieHall = movieHallMapper.selectById(movieHallId);
        movieHall.setRowsSeatCount(maxRowsSeatCount);
        movieHall.setColsSeatCount(maxColsSeatCount);
        movieHallMapper.updateById(movieHall);
    }


    @Override
    public Seat[][] wxGetSeatList(Long movieArrangeId) {
        Seat[][] list = getSeatList(movieArrangeMapper.selectById(movieArrangeId).getMovieHallId());

        if (list.length==0){
            return list;
        }

        List<Orders> hasBeenSelectOrders = ordersMapper.selectList(new QueryWrapper<Orders>().eq("movie_arrange_id",movieArrangeId));
        Long[] hasBeenSelectSeat = new Long[hasBeenSelectOrders.size()*6];
        String[] line = null;
        int s = 0;
        for (int i = 0; i<hasBeenSelectOrders.size();i++){
            line = hasBeenSelectOrders.get(i).getSeatId().split(",");
            for (int j = 0 ;j<line.length;j++){
                hasBeenSelectSeat[s] = Long.parseLong(line[j]);
                s++;
            }
        }


        for(int i=0 ; i<list.length ; i++){
            for (int j=0 ; j<list[0].length ; j++){
                for (int z = 0; z < hasBeenSelectSeat.length ; z++){
                    if (list[i][j].getId().equals(hasBeenSelectSeat[z])){
                        list[i][j].setStatus(0);
                    }
                }
            }
        }
        return list;
    }
}
